﻿
$(document).ready(function () {


    //** function to compare string with regex for checking word characters in string.
    //used to check for user name in login
    function isValidUserName(str) {
        return /^\w+$/.test(str);
    }
    //** function to compare string with regex for special characters in string
    // used for password field in login
    function isValidPassword(str) {
        return /^[0-9A-Za-z\s\`\~\!\@\#\$\%\^\&\*\(\)\+\=\{\}\;\:\,\.\<\>\_\?\-\[\]\/\|\']+$/.test(str);
    }



    //this is form submit event and called when login form is submitted.
    //in this first we check for valid values of username and password and call login function with parameters.
    $("#frm-login-id").submit(function (event) {

        //The event.preventDefault() method stops the default action of an element from happening.
        event.preventDefault();
        //check if username or paassword field is empty.
        if ($("#Password").val() == "" || $("#UserName").val() == "") {

            if ($("#UserName").val() == "") {
                $(".lg-error-cls").html("Username can not be blank.");
                $("#UserName").focus();
                return false;
            }
            if ($("#Password").val() == "") {
                $(".lg-error-cls").html("Password can not be blank.");
                $("#Password").focus();
                return false;
            }

        }
        //check if entered username is valid string
        if (!isValidUserName($("#UserName").val())) {
            $(".lg-error-cls").html("Invalid username and password.");
            return false;
        }
        //check if entered password is valid string
        if (!isValidPassword($("#Password").val())) {
            // alert("pass not valid");
            $(".lg-error-cls").html("Invalid username and password.");
            return false;
        }
        //call to logon function for login
        logon($("#UserName").val(), $("#Password").val());


        return true;
    });

    function customErrorMsg(errorMsg) {
        $(".custom-error-msg-cls").html(errorMsg);

    }

    //**** this function is used to login user into rtracker app.
    //in this first we make call to controller to validate user login credentials and check the status of user.
    //if user is already logged in then we show dailog prompt to confirm login here.
    //if user need to change password then we show change password dialog with message(reason for change password)
    //if everythng successfull then redirect it to vformty app.
    function logon(Uname, Psswd) {

        //check if parameters passed are null then assign the value from corresponding textbox.
        if (Uname == null || Uname == "null") {
            Uname = $("#UserName").val();
        }
        if (Psswd == null || Psswd == "null") {
            Psswd = $("#Password").val();
        }
        //ajax call to controller.
        $.ajax({
            type: 'POST',
            url: "/Login/Login",
            data: '{"pstrUserName":"' + Uname + '","pstrPassword":"' + Psswd + '"}',
            contentType: "application/json; charset=utf-8",
            success: function (msg) {

                //if somethng went wrong then Ocstatus will not be returned then show error msg.
                if (msg.oCStatus == null) {
                    $(".lg-error-cls").html(ServerErrorMsg);
                    $(".lg-error-cls").addClass("error");
                    return false;
                }
                //check if login status is true then 
                //check if it is already logged in then if there need to change password 
                // else redirect it to rtracker app.
                if (msg.oCStatus.Success == true) {
                    //check if it is already logged in then show dailog prompt to continue.
                    if (msg.oCStatus.StatusDetails == "AlreadyLoggedIn") {
                        $("#dialog-Already-loggedin").dialog("open");
                        return;
                    }

                    else {
                        //redirect to rtracker app.
                        window.location.replace("/");
                        //window.location.href = "/rtracker/Admin";
                    }
                }
                    //incase of false status set password policy variable to null and show the status details.
                else {

                    customErrorMsg(msg.oCStatus.StatusDetails); //Will display error message
                    PasswordPolicy = null;
                    $(".lg-error-cls").text(msg.oCStatus.StatusDetails);
                    return false;
                }
            },

            error: function (xhr, ajaxOptions, thrownError) {
                //                    alert("Error " + xhr.responseText);
                $(".lg-error-cls").html(ServerErrorMsg);
                return false;
            }

        });
    }
});



$("#btn-logout-id").live("click", function () {
    $.ajax({
        type: 'POST',
        url: "/Login/Logout",
        async: false,
        contentType: 'application/json; charset=utf-8',
        dataType: 'json',
        success: function (mydata) {
            if (mydata) {
                if (mydata.Success) {

                    //redirect to login page.
                    window.location.replace("/");
                    
                }
            }
        }
    });


});